﻿using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using umbraco;

namespace Our.Umbraco.uDbCompare
{
    public partial class detail : System.Web.UI.Page
    {
        private string currentConnectionString
        {
            get { return GlobalSettings.DbDSN; }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
               BindData();
        }

        protected void BackButton_Click(object sender, EventArgs e)
        {
            Response.Redirect(string.Format("index.aspx?id={0}", SessionVars.NodeId));
        }

        protected void RefreshButton_Click(object sender, EventArgs e)
        {
            BindData();
        }

        private void BindData()
        {
            DbCompareItem compareItem = (DbCompareItem)Enum.Parse(typeof(DbCompareItem), Request.QueryString["CompareItem"]);
            string key = Request.QueryString["name"];

            CompareTypeLabel.Text = compareItem.ToString();
            NameLabel.Text = key;

            var comparison = uDbCompare.CompareDetails(compareItem, key);
            
            if (DetailGridView.Columns.Count == 0)
            {
                BoundField name = new BoundField();
                name.DataField = "Name";
                name.HeaderText = "Name";
                DetailGridView.Columns.Add(name);

                BoundField left = new BoundField();
                left.DataField = "LeftData";
                left.HeaderText = SessionVars.GetServerNameAndDb(false, "Local Server");
                DetailGridView.Columns.Add(left);


                BoundField right = new BoundField();
                right.DataField = "RightData";
                right.HeaderText = SessionVars.GetServerNameAndDb(true, "Remote Server"); 
                DetailGridView.Columns.Add(right);
            }

            DetailGridView.RowDataBound += new GridViewRowEventHandler(DetailGridView_RowDataBound);
            DetailGridView.DataSource = comparison;
            DetailGridView.DataBind();
        }

        protected void DetailGridView_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                uDbCompareUI comparison = e.Row.DataItem as uDbCompareUI;
                if (comparison != null)
                {
                    if (comparison.LeftDataMissing)
                    {
                        SetCell(e.Row.Cells[1], DbCompareMatch.Missing);
                    }
                    else if (comparison.RightDataMissing)
                    {
                        SetCell(e.Row.Cells[2], DbCompareMatch.Missing);
                    }
                    else
                    {
                        if (comparison.DataMatches())
                            SetCell(e.Row.Cells[0], DbCompareMatch.Matching);
                        else
                            SetCell(e.Row.Cells[0], DbCompareMatch.NonMatching);
                    }
                }
            }
        }

        private void SetCell(TableCell cell, DbCompareMatch match)
        {
            switch (match)
            {
                case DbCompareMatch.Matching:
                    cell.BackColor = System.Drawing.Color.Green;
                    break;
                case DbCompareMatch.Missing:
                    cell.BackColor = System.Drawing.Color.Red;
                    break;
                case DbCompareMatch.NonMatching:
                    cell.BackColor = System.Drawing.Color.Yellow;
                    break;
            }
        }

    }
}
